package com.mars.manager.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mars.common.i18n.I18nMessage;
import com.mars.component.mail.MailUtils;
import com.mars.manager.common.BaseController;
import com.mars.manager.entity.Menu;
import com.mars.manager.exception.MenuBizException;
import com.mars.manager.service.MenuService;
import com.mars.web.common.pojo.ResponseResult;

@Controller
@RequestMapping(value = "/test")
public class TestController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(getClass()) ;
	
	@Autowired
	private MenuService menuService ;
	
	@Autowired
	private MailUtils mailUtils ;
	
	
	@RequestMapping(value = "/main")
	public String main() {
		return "index";
	}

	@RequestMapping(value = "/login")
	public String login() {
		logger.info("11111111111111111112222222222222222");
		System.out.println("======login===11=======");
		return "login";
	}
	
	@RequestMapping(value = "/test")
	@ResponseBody
	public void test() {
		System.out.println("============================="+LocaleContextHolder.getLocale());
		System.out.println("I18N-Controller===:"+I18nMessage.getMessage("HTTPCODE_200"));;
		List<Menu> menuCategory;
		try {
			menuCategory = menuService.getMenuCategory();
			System.out.println("======test===11======="+menuCategory);
		} catch (MenuBizException e) {
			System.out.println(e.getMsg()+"=="+e.getCode());
			//e.printStackTrace();
		}
		this.menuService.getMenuByParentId(1l);
	}

	@RequestMapping(value = "/hello", produces = "text/plain;charset=UTF-8")
	public @ResponseBody
	String hello() {
		return "你好！hello";
	}
	
	@RequestMapping(value = "/say2/{msg}", produces = "text/plain;charset=UTF-8")
	public @ResponseBody String say(@PathVariable(value = "msg") String msg) {
		return "{\"msg\":\"you say:'" + msg + "'\"}";
	}

	@RequestMapping(value = "/say/{msg}", produces = "application/json;charset=UTF-8")
	public @ResponseBody String say2(@PathVariable(value = "msg") String msg) {
		return "{\"msg\":\"you say:'" + msg + "'\"}";
	}
	
	@RequestMapping(value = "/list")
	@ResponseBody
	public Object list() {
		List<TestEntity> list = new ArrayList<TestEntity>();
		list.add(new TestEntity(1, "杨浩泉1", "广州1"));
		list.add(new TestEntity(2, "杨浩泉2", "广州2"));
		list.add(new TestEntity(3, "杨浩泉3", "广州3"));
		list.add(new TestEntity(4, "杨浩泉4", "广州4"));
		return list;
	}

	@RequestMapping(value = "/list2")
	@ResponseBody
	public Object list2() {
		List<TestEntity> list = new ArrayList<TestEntity>();
		list.add(new TestEntity(1, "杨浩泉1", "广州1"));
		list.add(new TestEntity(2, "杨浩泉2", "广州2"));
		list.add(new TestEntity(3, "杨浩泉3", "广州3"));
		list.add(new TestEntity(4, "杨浩泉4", "广州4"));
		return setResponseResult(200, "成功", list) ;
		//return list;
	}

	@RequestMapping(value = "/sendMail")
	@ResponseBody
	public Object sendMail() {
		List<TestEntity> list = new ArrayList<TestEntity>();
		list.add(new TestEntity(1, "杨浩泉1", "广州1"));
		list.add(new TestEntity(2, "杨浩泉2", "广州2"));
		list.add(new TestEntity(3, "杨浩泉3", "广州3"));
		list.add(new TestEntity(4, "杨浩泉4", "广州4"));
		
		Map<String, Object> map = new HashMap<String, Object>() ;
		map.put("userlist", list) ;
		mailUtils.sendMail("yhaoquan@163.com", "template/test.ftl", map) ;
		return list;
	}

	@RequestMapping(value = "/list3")
	@ResponseBody
	public Object list3() {
		List<TestEntity> list = new ArrayList<TestEntity>();
		list.add(new TestEntity(1, "杨浩泉1", "广州1"));
		list.add(new TestEntity(2, "杨浩泉2", "广州2"));
		list.add(new TestEntity(3, "杨浩泉3", "广州3"));
		list.add(new TestEntity(4, "杨浩泉4", "广州4"));
		//ResponseResult ok = ResponseResult.ok("123") ;
		//System.out.println(JSON.toJSON(ok));
		//return ResponseResult.faild("123");
		//return setAjaxMapSuccess();
		System.out.println(HttpStatus.NOT_FOUND.name());
		//return setResponseResult(200, "消息", "用户管理", null, HttpStatus.NOT_FOUND) ;
		//return setResponseResultOk(list) ;
		//return setResponseResult(HttpStatus.NOT_FOUND, null, null, null);
		//return setResponseResult(HttpStatus.NOT_FOUND, null);
		//System.out.println(JSON.toJSON(setResponseResultError(HttpStatus.NOT_FOUND, "无效的连接")));
		//System.out.println(JSON.toJSON(setResponseResultOk("123")));
		//return setResponseResultError(HttpStatus.NOT_FOUND) ;
		
		//return ResponseResult.ok("1111111111", list);
		//return ResponseResult.faild("1111111111");
		System.out.println(I18nMessage.getMessage("HTTPCODE_401"));
		return ResponseResult.build(300, "Hello");
		
	}
	
	@RequestMapping(value = "/returnMsg")
	@ResponseBody
	public Object returnMsg() {
		
		//return setResponseResult(500, true, "cascas");
		//return setResponseResultError(HttpStatus.EXPECTATION_FAILED);
		//return setResponseResultAddOk();
		//return setResponseResultOk();
		return setResponseResult(HttpStatus.EXPECTATION_FAILED, "1111");
		
		//return setResponseResultFailed("Hello");
	}

}
